#include "oigraficos.h"

SDL_Surface *janela=NULL;
SDL_Surface **imagens=NULL;
int nimagens=0;
char lastEnter = 0;

void oigraficosCriaJanela (int w,int h, string windowName) {
	SDL_Init(SDL_INIT_EVERYTHING);
	janela = SDL_SetVideoMode (w,h,32, SDL_HWSURFACE);
	SDL_WM_SetCaption (windowName.c_str(),NULL);
	SDL_EnableUNICODE (1);
}

void oigraficosCarregaImagens (string caminhos[],int size) {
	nimagens = size;
	imagens = (SDL_Surface**)malloc (size*sizeof(SDL_Surface*));
	
	int i;
	for (i=0;i<size;i++) {
		imagens[i] = IMG_Load (caminhos[i].c_str());
	}
}

void oigraficosDesenhaImagem (int indice,int x,int y) {
	SDL_Rect dest;
	dest.x = x;
	dest.y = y;

	SDL_BlitSurface(imagens[indice], NULL, janela, &dest);
}

void oigraficosMain () {
	SDL_Flip (janela);
	
	SDL_Event evt;
	
	while (SDL_PollEvent(&evt) != 0) {
		switch (evt.type) {
			case SDL_QUIT:
				exit(0);
			break;
			case SDL_KEYDOWN:
				lastEnter = evt.key.keysym.unicode;
			break;
		}
	}
}

char oigraficosLeEntrada () {
	return lastEnter;
}

void oigraficosFechaJanela () {
	int i;
	for (i=0;i<nimagens;i++)
		SDL_FreeSurface (imagens[i]);
	free(imagens);

	SDL_Quit();
}
